package wxopen_api

import (
	"context"
	"fmt"
	"net/http"
)

type EmWxaEntityVerifyType int

const (
	VerifyType_Personal   EmWxaEntityVerifyType = 0
	VerifyType_Enterprise EmWxaEntityVerifyType = 1
	VerifyType_Goverment  EmWxaEntityVerifyType = 2
	VerifyType_Media      EmWxaEntityVerifyType = 3
	VerifyType_OtherOrg   EmWxaEntityVerifyType = 4
)

type CategoryItemQualifyInnerItem struct {
	Name string `json:"name"`
	Url  string `json:"url"`
}

type CategoryItemQualifyExterItem struct {
	InnerList []CategoryItemQualifyInnerItem `json:"inner_list"`
}

type CategoryItemQualify struct {
	ExterList []CategoryItemQualifyExterItem `json:"exter_list"`
}

type CategoryItem struct {
	ID            int                 `json:"id"`
	Name          string              `json:"name,omitempty"`
	Level         int                 `json:"level,omitempty"`
	Father        int                 `json:"father,omitempty"`
	Children      []int               `json:"children,omitempty"`
	SensitiveType int                 `json:"sensitive_type,omitempty"`
	Qualify       CategoryItemQualify `json:"qualify,omitempty"`
}

type CategoriesListItem struct {
	Categories []CategoryItem `json:"categories,omitempty"`
}

type GetAllCategoriesResponse struct {
	CategoriesList []CategoriesListItem `json:"categories_list,omitempty"`
}

// AddCategory 添加类目信息
// Wechat API docs:
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/category/getallcategories.html
func (s *WXAService) GetAllCategories(ctx context.Context, token string) (*GetAllCategoriesResponse, error) {
	u := fmt.Sprintf("cgi-bin/wxopen/getallcategories?access_token=%v", token)
	req, err := s.client.NewRequest(http.MethodGet, u, nil)
	if err != nil {
		return nil, err
	}
	getAllCategoriesResponse := new(GetAllCategoriesResponse)
	_, err = s.client.Do(ctx, req, getAllCategoriesResponse)
	if err != nil {
		return nil, err
	}
	return getAllCategoriesResponse, nil
}

type GetAllCategoriesByTypeRequest struct {
	VerifyType int `json:"verify_type,omitempty"`
}

// AddCategory 添加类目信息
// Wechat API docs:
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/category/getcategorybytype.html
func (s *WXAService) GetAllCategoriesByType(ctx context.Context, token string, r *GetAllCategoriesByTypeRequest) (*GetAllCategoriesResponse, error) {
	u := fmt.Sprintf("cgi-bin/wxopen/getcategoriesbytype?access_token=%v", token)
	req, err := s.client.NewRequest(http.MethodPost, u, r)
	if err != nil {
		return nil, err
	}
	getAllCategoriesResponse := new(GetAllCategoriesResponse)
	_, err = s.client.Do(ctx, req, getAllCategoriesResponse)
	if err != nil {
		return nil, err
	}
	return getAllCategoriesResponse, nil
}

type CerticateItem struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type AddCategoryItem struct {
	First  int64 `json:"first,omitempty"`
	Second int64 `json:"second,omitempty"`
	// 资质证书
	Certicates []CerticateItem `json:"certicates,omitempty"`
}

// AddCategoryRequest 添加类目信息
type AddCategoryRequest struct {
	Categories []AddCategoryItem `json:"categories"`
}

// AddCategory 添加类目信息
// Wechat API docs:
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/category/addcategory.html
func (s *WXAService) AddCategory(ctx context.Context, token string, r *AddCategoryRequest) error {
	u := fmt.Sprintf("cgi-bin/wxopen/addcategory?access_token=%v", token)
	req, err := s.client.NewRequest(http.MethodPost, u, r)
	if err != nil {
		return err
	}
	_, err = s.client.Do(ctx, req, nil)
	if err != nil {
		return err
	}
	return nil
}

type AddedCategoryItem struct {
	First       int64  `json:"first,omitempty"`
	FirstName   string `json:"first_name,omitempty"`
	Second      int64  `json:"second,omitempty"`
	SecondName  string `json:"second_name,omitempty"`
	AuditStatus int64  `json:"audit_status,omitempty"`
	AuditReason string `json:"audit_reason,omitempty"`
}

// GetCategoryResponse 获取小程序类目返回
type GetCategoryResponse struct {
	Categories    []AddedCategoryItem `json:"categories,omitempty"`
	Limit         int32               `json:"limit,omitempty"`
	Quota         int32               `json:"quota,omitempty"`
	CategoryLimit int32               `json:"category_limit,omitempty"`
}

// GetCategory 获取小程序类目
// Wechat API docs:
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/category/getcategory.html
func (s *WXAService) GetCategory(ctx context.Context, token string) (*GetCategoryResponse, error) {
	u := fmt.Sprintf("cgi-bin/wxopen/getcategory?access_token=%v", token)
	req, err := s.client.NewRequest(http.MethodGet, u, nil)
	if err != nil {
		return nil, err
	}
	resp := new(GetCategoryResponse)
	_, err = s.client.Do(ctx, req, resp)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
